import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

main() {
  runApp(DBJApp());
}

class DBJCounterWidget extends InheritedWidget {
  final int? counter;
  DBJCounterWidget({required Widget child, this.counter}) : super(child: child);

  static DBJCounterWidget? of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType();
  }

  @override
  bool updateShouldNotify(covariant DBJCounterWidget oldWidget) {
    return oldWidget.counter != counter;
  }
}

class DBJApp extends StatelessWidget {
  const DBJApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primaryColor: Colors.orange, splashColor: Colors.transparent),
      home: DBJHomePage(),
    );
  }
}

class DBJHomePage extends StatefulWidget {
  const DBJHomePage({Key? key}) : super(key: key);

  @override
  _DBJHomePageState createState() => _DBJHomePageState();
}

class _DBJHomePageState extends State<DBJHomePage> {
  int counter = 29;

  @override
  Widget build(BuildContext context) {
    print("_DBJHomePageState ----- build");
    return Scaffold(
      appBar: AppBar(
        title: Text('打扮家'),
      ),
      body: Center(
        child: DBJCounterWidget(
          counter: counter,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              DBJCardWidget(),
              DBJContainerWidget(),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          print('点击了按钮');
          // counter++;
          setState(() {
            counter++;
          });
        },
        child: Icon(
          Icons.add,
          color: Colors.white,
        ),
      ),
    );
  }
}

class DBJCardWidget extends StatefulWidget {
  const DBJCardWidget({Key? key}) : super(key: key);

  @override
  _DBJCardWidgetState createState() => _DBJCardWidgetState();
}

class _DBJCardWidgetState extends State<DBJCardWidget> {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print("_DBJCardWidgetState ----- didChangeDependencies");
  }

  @override
  Widget build(BuildContext context) {
    print("DBJCardWidget ----- build");
    int? counter1 = DBJCounterWidget.of(context)?.counter;
    return Card(
      color: Colors.purple,
      child: Text(
        '当前计数：$counter1',
        style: TextStyle(fontSize: 20, color: Colors.white),
      ),
    );
  }
}

class DBJContainerWidget extends StatefulWidget {
  const DBJContainerWidget({Key? key}) : super(key: key);

  @override
  _DBJContainerWidgetState createState() => _DBJContainerWidgetState();
}

class _DBJContainerWidgetState extends State<DBJContainerWidget> {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print("_DBJContainerWidgetState ----- didChangeDependencies");
  }

  @override
  Widget build(BuildContext context) {
    print("DBJContainerWidget ----- build");
    int? counter2 = DBJCounterWidget.of(context)?.counter;
    return Container(
      color: Colors.orange,
      child: Text(
        '当前计数：$counter2',
        style: TextStyle(fontSize: 20, color: Colors.white),
      ),
    );
  }
}
